﻿using AdminSystem.Applications;
using AdminSystem.Domain.User;
using AdminSystem.Repositories;
using AdminSystem.Write.API.Command.UserCommand;
using AdminSystem.Write.API.DTOS;
using MediatR;

namespace AdminSystem.Write.API.Handers.UserHander
{
    public class UserLoginHandler : IRequestHandler<UserLoginCommand, ApiResult<List<UserDTO>>>
    {
        private readonly IRepository<UserInfo> userInfo;
        private readonly IRepository<Role> repository;

        public UserLoginHandler(IRepository<UserInfo> userInfo, IRepository<Role> repository)
        {
            this.userInfo = userInfo;
            this.repository = repository;
        }
        /// <summary>
        /// 登录异步方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<ApiResult<List<UserDTO>>> Handle(UserLoginCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<UserDTO>> result = new ApiResult<List<UserDTO>>();
                result.statusCode = StatusCode.FaieId;
                result.Message = "登录成功";
                var users = await userInfo.GetAll();
                var roles = await repository.GetAll();
                var user = from a in users
                           join b in roles on a.RoleId equals b.RoleId
                           where a.UserName == request.UserName
                           select new UserDTO
                           {
                               UserId = a.UserId,
                               UserName = a.UserName,
                               UserPass = a.UserPass,
                               UserNickname = a.UserNickname,
                               RoleId = a.RoleId,
                               RoleName = b.RoleName
                           };
                var entity = user.FirstOrDefault();
                //var user = await userInfo.QueryFirstAsync(x => x.UserName == request.UserName);
                if (entity == null)
                {
                    result.statusCode = StatusCode.FaieId;
                    result.Message = "用户不存在";
                    return result;
                }
                if (entity.UserPass != request.Password)
                {
                    result.statusCode = StatusCode.FaieId;
                    result.Message = "密码错误";
                    return result;
                }
                // Assuming you want to return user details in the result
                result.statusCode = StatusCode.Success;
                result.Data = new List<UserDTO> { new UserDTO { UserId = entity.UserId, UserName = entity.UserName, UserPass = entity.UserPass, UserNickname = entity.UserNickname, RoleId = entity.RoleId, RoleName = entity.RoleName } };
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred while processing the login request.", ex);
            }
        }
    }
}
